package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.text.SimpleDateFormat;
import java.util.*;


@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService{
    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if(password!=null){
            String new_password = MD5Utils.md5(password);
            member.setPassword(new_password);
        }
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberAmountByMouths(List<String> months) {
        List<Integer> list=new ArrayList<>();

        for (String month : months) {
            String date=month+".31";
            Integer count = memberDao.findMemberCountBeforeDate(date);
            list.add(count);
        }
        return list;
    }

    @Override

    public List<Map<String, Object>> findMemberSexCount() {
        return memberDao.findMemberSexCount();
    }



    public List<Map<String, Object>> findAgeCount() {

        //分类：0-18、18-30、30-45、45以上
        //0-18
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        Date today = DateUtils.getToday();
        String now = sdf.format(today);
        Calendar calendar=Calendar.getInstance();

        //获取18年前日期,当前日期往前推18年
        calendar.add(Calendar.YEAR,-18);
        Date date = calendar.getTime();
        String time18 = sdf.format(date);

        //获取30年前日期，18年前往前推12年
        calendar.add(Calendar.YEAR,-12);
        Date date1 = calendar.getTime();
        String time30 = sdf.format(date1);

        //获取45年前日期，30年前往前推15年
        calendar.add(Calendar.YEAR,-15);
        Date date2 = calendar.getTime();
        String time45 = sdf.format(date2);

        //18岁以下的数量
        Integer count1 = memberDao.findAgeCountAfterDate(time18);
        //18-30岁的人数
        Map<String,String> map = new HashMap<>();
        map.put("start",time30);
        map.put("end",time18);
        Integer count2 = memberDao.findMemberCountBetweenDate(map);

        //30-45岁的人数
        Map<String,String> map1 = new HashMap<>();
        map1.put("start",time45);
        map1.put("end",time30);
        Integer count3 = memberDao.findMemberCountBetweenDate(map1);

        //45岁以上的人数
        Integer count4 = memberDao.findAgeCountBeforeDate(time45);

        Map<String,Object> m=new HashMap<>();
        m.put("name","0-18岁");
        m.put("value",count1);
        Map<String,Object> m1=new HashMap<>();
        m1.put("name","18-30岁");
        m1.put("value",count2);
        Map<String,Object> m2=new HashMap<>();
        m2.put("name","30-45岁");
        m2.put("value",count3);
        Map<String,Object> m3=new HashMap<>();
        m3.put("name","45岁以上");
        m3.put("value",count4);

        List<Map<String,Object>> list=new ArrayList<>();
        list.add(m);
        list.add(m1);
        list.add(m2);
        list.add(m3);

        return list;
    }







    @Override
    public Integer findMemberCountBySameYearAndMonth(String startTime, String endTime) {
        Map map=new HashMap();
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        return memberDao.findMemberCountBySameYearAndMonth(map);
    }


}
